package com.hsgene.authority.controller;

import com.hsgene.authority.constants.AccountPermissionCondition;
import com.hsgene.authority.domain.GeneticTestingOperationPermission;
import com.hsgene.authority.dto.OperationAccountPermissionDto;
import com.hsgene.authority.dto.OperationPermissionTree;
import com.hsgene.authority.dto.OperationPermissionUpdateDto;
import com.hsgene.authority.exception.AuthorityErrorCode;
import com.hsgene.authority.service.OperationAccountPermissionService;
import com.hsgene.authority.service.PermissionService;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.Pagination;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限管理Controller
 *
 * @author wangbing
 * @version 1.0, 2018/11/1
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @Resource
    private OperationAccountPermissionService operationAccountPermissionService;

    /**
     * 查询权限列表
     *
     * @param companyName    公司名称
     * @param departmentName 部门名称
     * @param searchKey      搜索关键字：权限名称
     * @param page           页号
     * @param size           每页大小
     * @return 权限列表
     */
    @GetMapping("")
    public ApiResult<Pagination<OperationAccountPermissionDto>> queryPermissions(
            @RequestParam(name = "companyName", required = false) String companyName,
            @RequestParam(name = "departmentName", required = false) String departmentName,
            @RequestParam(name = "searchKey", required = false) String searchKey,
            @RequestParam("page") int page,
            @RequestParam("size") int size) {
        AccountPermissionCondition condition = new AccountPermissionCondition();
        condition.setCompanyName(companyName);
        condition.setDepartmentName(departmentName);
        condition.setSearchKey(searchKey);
        LimitHelper limitHelper = LimitHelper.create(page, size);

        return operationAccountPermissionService.queryPermissions(condition, limitHelper);
    }

    /**
     * 添加权限
     *
     * @param permission 权限信息
     * @return 操作结果
     */
    @PostMapping("")
    public ApiResult<NullObject> addPermission(
            @RequestBody GeneticTestingOperationPermission permission) {
        return permissionService.addPermission(permission);
    }

    /**
     * 获取权限信息
     *
     * @param permissionId 权限id（当获取根节点信息时，传入固定值：root）
     * @param wholeTree    返回控制参数（true：返回以当前节点为根的完整权限树，false：返回当前节点的直接下级节点信息）
     * @return 权限信息
     */
    @GetMapping("{permission_id}")
    public ApiResult<OperationPermissionTree> getPermissionTree(
            @PathVariable("permission_id") String permissionId,
            @RequestParam(name = "wholeTree", defaultValue = "false") Boolean wholeTree) {
        OperationPermissionTree result = permissionService.getPermissionTree(permissionId, wholeTree);
        if (result == null) {
            return ApiResult.fail(AuthorityErrorCode.PERMISSION_NOT_EXISTED);
        }
        return ApiResult.succ(result);
    }

    /**
     * 修改权限
     *
     * @param permissionId 权限id
     * @param permission   权限信息
     * @return 操作结果
     */
    @PutMapping("{permission_id}")
    public ApiResult<NullObject> updatePermission(
            @PathVariable("permission_id") String permissionId,
            @RequestBody GeneticTestingOperationPermission permission) {
        permission.setId(permissionId);
        return permissionService.updatePermission(permission);
    }

    /**
     * 删除权限
     *
     * @param permissionId 权限id
     * @return 操作结果
     */
    @DeleteMapping("{permission_id}")
    public ApiResult<NullObject> deletePermission(
            @PathVariable("permission_id") String permissionId) {
        return permissionService.deletePermission(permissionId);
    }

    /**
     * 获取某个部门的详细权限信息
     *
     * @param departmentId 部门id
     * @return 详细权限信息
     */
    @GetMapping("department/{department_id}")
    public ApiResult<OperationPermissionTree> findDepartmentPermission(
            @PathVariable("department_id") String departmentId) {
        return operationAccountPermissionService.findDepartmentPermission(departmentId, true, false);
    }

    /**
     * 更新某个部门的权限
     *
     * @param departmentId 部门id
     * @param dto          选中的权限信息
     * @return 更新结果
     */
    @PutMapping("department/{department_id}")
    public ApiResult<NullObject> updateDepartmentPermission(
            @PathVariable("department_id") String departmentId,
            @RequestBody OperationPermissionUpdateDto dto) {
        return operationAccountPermissionService.updateDepartmentPermission(departmentId, dto.getIds());
    }

    /**
     * 初始化部门权限
     *
     * @param departmentId 部门id
     * @param initType     初始化类型（init：新增初始化，clear：删除初始化）
     * @return 更新结果
     */
    @PutMapping("department/{department_id}/initialization")
    public ApiResult<NullObject> initDepartmentPermission(
            @PathVariable("department_id") String departmentId,
            @RequestParam(name = "initType", defaultValue = "init") String initType) {
        return operationAccountPermissionService.initDepartmentPermission(departmentId, initType);
    }
}
